<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="姓名">
              <a-input v-model="queryParam.id" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="审核状态">
              <a-select v-model="queryParam.status" placeholder="请选择" default-value="0">
                <a-select-option value="0">已审核</a-select-option>
                <a-select-option value="1">未审核</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="8" :sm="24">
              <a-form-item label="手机号码">
                <a-input-number v-model="queryParam.callNo" style="width: 100%"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="更新日期">
                <a-date-picker v-model="queryParam.date" style="width: 100%" placeholder="请输入更新日期"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="!advanced && 8 || 24" :sm="24">
            <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
              <a @click="toggleAdvanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="add">添加新考生</a-button>
      <a-button type="primary" icon="export" @click="exportData">导出</a-button>
      <a-dropdown v-action:edit v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="2"><a-icon type="confirm" />确认审核</a-menu-item>
          <a-menu-item key="3"><a-icon type="cancel" />取消审核</a-menu-item>
          <a-menu-item key="1"><a-icon type="delete" />删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作 <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <s-table
      ref="table"
      size="default"
      rowKey="objectId"
      :scroll="{ x: 2300, y: scrollHeight }"
      :columns="columns"
      :data="loadData"
      :rowSelection="options.rowSelection"
      showPagination="auto"
    >
      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
      <span slot="pay" slot-scope="text, record">
        <a-tag :color="record.pay?'purple':'orange'">{{ record.pay?'已支付':'未支付' }}</a-tag>
      </span>
      <span slot="img" slot-scope="text, record">
        <a-avatar style="cursor: zoom-in" :size="32" :src="record.imgUrl" shape="square" @click="preview(record.imgUrl)" />
      </span>
      <span slot="personalProfile" slot-scope="text">
        <ellipsis :length="14" tooltip>{{ text }}</ellipsis>
      </span>

      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleView(record)">审查</a>
          <a-divider type="vertical" />
          <a @click="handleUpdateSignUp(record)">{{ record.qualifications? '取消': '确认' }}</a>
          <a-divider type="vertical" />
          <a @click="handleDelete(record)">删除</a>
        </template>
      </span>
    </s-table>
    <create-form ref="createModal" @ok="handleOk" />
    <view-form :columns="columns" ref="viewModal"></view-form>
    <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel">
      <img alt="example" style="width: 100%" :src="previewImage" />
    </a-modal>
  </a-card>
</template>

<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components'
import StepByStepModal from './modules/StepByStepModal'
import CreateForm from './modules/CreateForm'
import ViewForm from './modules/ViewForm'
// import { getRoleList, getServiceList } from '@/api/manage'
import av from '../../api/av'
import axios from '@/utils/request'
import ExportJsonExcel from 'js-export-excel'

const statusMap = {
  0: {
    status: 'default',
    text: '关闭'
  },
  1: {
    status: 'processing',
    text: '运行中'
  },
  2: {
    status: 'success',
    text: '已上线'
  },
  3: {
    status: 'error',
    text: '异常'
  }
}

export default {
  name: 'TableList',
  components: {
    STable,
    Ellipsis,
    CreateForm,
    StepByStepModal,
    ViewForm
  },
  computed: {
    scrollHeight () {
      const width = window.innerWidth
      console.log(width)
      if (width > 1400) {
        return 600
      }
      return 500
    }
  },
  data () {
    return {
      previewVisible: false,
      previewImage: '',
      mdl: {},
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      tableData: [],
      // 表头
      columns: [
        { title: '#', width: 60, scopedSlots: { customRender: 'serial' } },
        { title: '姓名', dataIndex: 'name', width: 100 },
        { title: '身份证号', dataIndex: 'idNum', width: 180 },
        { title: '性别', dataIndex: 'gender', width: 60 },
        { title: '出生年月', dataIndex: 'birth', width: 120 },
        { title: '籍贯', dataIndex: 'birthplace', width: 140 },
        { title: '民族', dataIndex: 'minzu', width: 60 },
        { title: '学历层次', dataIndex: 'education', width: 100 },
        { title: '照片', dataIndex: 'imgUrl', width: 100, scopedSlots: { customRender: 'img' } },
        { title: '支付状态', dataIndex: 'pay', scopedSlots: { customRender: 'pay' } },
        { title: '毕业时间', dataIndex: 'graduation', width: 120 },
        { title: '院校', dataIndex: 'school', width: 160 },
        { title: '专业', dataIndex: 'profession', width: 120 },
        { title: '报考单位', dataIndex: 'baokaodanwei', width: 180 },
        { title: '岗位代码', dataIndex: 'jobCode', width: 120 },
        { title: '毕业证书编号', dataIndex: 'diplomaNumber', width: 180 },
        { title: '个人简介',
          dataIndex: 'personalProfile',
          width: 120,
          scopedSlots: { customRender: 'personalProfile' } },
        // {
        //   title: '服务调用次数',
        //   dataIndex: 'callNo',
        //   sorter: true,
        //   needTotal: true,
        //   customRender: (text) => text + ' 次'
        // },
        // {
        //   title: '更新时间',
        //   dataIndex: 'updatedAt',
        //   width: 120,
        //   sorter: true
        // },
        {
          title: '操作',
          dataIndex: 'action',
          width: '150px',
          fixed: 'right',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        // return av.querySignUpList(Object.assign(parameter, this.queryParam))
        return av.query('signUp', parameter)
          .then(async res => {
            const count = await av.queryCount('signUp')
            const totalPage = count / parameter.pageSize
            const result = {
              pageNo: parameter.pageNo,
              pageSize: parameter.pageSize,
              totalCount: count,
              totalPage: Math.ceil(totalPage)
            }
            result.data = res.map(v => {
              return {
                ...v._serverData,
                objectId: v.id
              }
            })
            this.tableData = result.data
            return result
          })
      },
      selectedRowKeys: [],
      // custom table alert & rowSelection
      options: {
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      }
    }
  },
  filters: {
    statusFilter (type) {
      return statusMap[type].text
    },
    statusTypeFilter (type) {
      return statusMap[type].status
    }
  },
  created () {
    this.tableOption()
  },
  methods: {
    preview (url) {
      this.previewImage = url
      this.previewVisible = true
    },
    handleCancel () {
      this.previewVisible = false
    },
    add () {
      // this.$refs.createModal.add()
      window.open('/putonghuabaoming/#/baoming/home')
    },
    exportData () {
      av.exportData('signUp').then(res => {
        console.log(res)
        var option = {}

        option.fileName = 'excel'
        option.datas = [
          {
            sheetData: res.map(v => v._serverData),
            sheetHeader: this.columns.slice(1, this.columns.length - 2).map(v => v.title),
            columnWidths: [18, 18]
          }
        ]

        var toExcel = new ExportJsonExcel(option) // new
        toExcel.saveExcel() // 保存
      })
    },
    successMsgRefresh (text) {
      this.$message.success(text + '成功')
      this.refresh()
    },
    handleUpdateSignUp (record) {
      console.log(record)
      av.updateSignUp(record.objectId, !record.qualifications).then(res => {
        this.successMsgRefresh('修改')
      })
    },
    handleDelete (record) {
      av.delete('signUp', record.objectId).then(res => {
        this.successMsgRefresh('删除')
      })
    },
    handleView (record) {
      this.$refs.viewModal.visible = true
      this.$refs.viewModal.record = record
    },
    refresh () {
      this.$refs.table.refresh()
    },
    tableOption () {
      this.options = {
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      }
    },

    handleEdit (record) {
      console.log(record)
      this.$refs.createModal.edit(record)
    },
    handleOk () {
      this.$refs.table.refresh()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    resetSearchForm () {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}
</script>

<style lang="less">
  .table-operator{
    margin-bottom: 18px;
    button{
      margin-right: 8px;
    }
  }
</style>
